package com.mproject.activity.project;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.mproject.activity.R;
import com.mproject.adapter.GridCellAdapter;
import com.mproject.adapter.Project_Schedule_Event_List_Adapter;
import com.mproject.customclass.Event;

public class Project_Schedule_Month extends Activity implements OnClickListener{

	private Calendar date;
	private TextView back_button;
	private TextView next_button;
	private TextView current_month_year_text;
	private int month;
	private int year;

	private ListView eventList;

	private GridView calendarView;
	private GridCellAdapter adapter;

	private static final String dateTemplate = "MMMM yyyy";
	private final DateFormat dateFormatter = new DateFormat();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.project_schedule_month_view); 

		calendarView = (GridView)findViewById(R.id.project_schedule_calendar);
		back_button = (TextView)findViewById(R.id.project_schedule_back_calendar);
		back_button.setOnClickListener(this);
		next_button = (TextView)findViewById(R.id.project_schedule_next_calendar);
		next_button.setOnClickListener(this);
		current_month_year_text = (TextView)findViewById(R.id.current_month_year_text);

		date = Calendar.getInstance(Locale.getDefault());
		month = date.get(Calendar.MONTH)+1;
		year = date.get(Calendar.YEAR);
		current_month_year_text.setText(dateFormatter.format(dateTemplate, date.getTime()));

		adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, true);
		adapter.notifyDataSetChanged();
		calendarView.setAdapter(adapter);
		calendarView.setEnabled(false);

		eventList = (ListView)findViewById(R.id.project_schedule_list_event);
		
		List<Event> events = new ArrayList<Event>();
		events.add(new Event("Group Assignment 2", "7 AM"));
		events.add(new Event("Design Analysis Performance Work", "11 PM"));
		events.add(new Event("Design Analysis Performance Work", ""));
		Project_Schedule_Event_List_Adapter event_adapter = new Project_Schedule_Event_List_Adapter(Project_Schedule_Month.this,getApplicationContext(), R.layout.project_schedule_event_list_item,events);
		eventList.setAdapter(event_adapter);
		eventList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> a, View v, int pos, long id){
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == back_button)
		{
			if (month <= 1)
			{
				month = 12;
				year--;
			}
			else
			{
				month--;
			}
			//Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: " + month + " Year: " + year);
			setGridCellAdapterToDate(month, year, false);
		}
		if (v == next_button)
		{
			if (month > 11)
			{
				month = 1;
				year++;
			}
			else
			{
				month++;
			}
			//	Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: " + month + " Year: " + year);
			setGridCellAdapterToDate(month, year, false);
		}

	}

	private void setGridCellAdapterToDate(int month, int year, boolean isFirstTime)
	{
		adapter = new GridCellAdapter(getApplicationContext(), R.id.calendar_day_gridcell, month, year, isFirstTime);
		date.set(year, month-1, date.get(Calendar.DAY_OF_MONTH));
		current_month_year_text.setText(dateFormatter.format(dateTemplate, date.getTime()));
		adapter.notifyDataSetChanged();
		calendarView.setAdapter(adapter);
		/*Log.e(tag,"next or previous button clicked "+year+"-"+month+"-"+((_calendar.get(Calendar.DAY_OF_MONTH)>9)?_calendar.get(Calendar.DAY_OF_MONTH):"0"+_calendar.get(Calendar.DAY_OF_MONTH)));
		updateDisplay(year+"-"+(month)+"-"+((_calendar.get(Calendar.DAY_OF_MONTH)>9)?_calendar.get(Calendar.DAY_OF_MONTH):"0"+_calendar.get(Calendar.DAY_OF_MONTH)));*/
		//"2012-04-12"
	}
}
